<template>
  <div class="toolbar-wrap">
    <span class="text" v-show="mode === 'edit'">{{ text }}</span>
    <div class="toolbar">
      <a-tooltip 
        v-for="(item, index) in toolList" 
        :key="index" 
        effect="dark" 
        :content="item.desc" 
        placement="bottom" 
        popper-class="logic-tooltip-pop"
      >
        <span 
          class="tool-item"
          :class="[currentTool===item.name && 'selected', item.disabled && 'disabled']" 
          @click.stop="handleClick(item, $event)" 
        >
          <img :src="item.icon">
        </span>
      </a-tooltip>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, Ref, ref, unref, defineProps, watch, defineExpose, defineEmits , computed} from 'vue';
import { toolMap } from '../../util/typeMap'
import { EDITOR_EVENT } from '../../util/constant'

const text = ref('根据需求配置执行流程图');
const currentTool = ref('');
const undoAble = ref(false);
const redoAble = ref(false);

// 定义组件接收的参数
const props = defineProps({
  lf: Object,
  graph: Object,
  mode: {
    type: String,
    default: 'edit'
  }
});

const toolList = computed(()=>{
  return Object.values(toolMap).map(tool => {
        switch(tool.name) {
          case 'undo':
            tool.disabled = !undoAble
            break
          case 'redo':
            tool.disabled = !redoAble
            break
          case 'beautify':
            tool.disabled = false
            break
          case 'selection':
            tool.disabled = props.mode === 'preview'
            break
          case 'navigation':
            tool.disabled = props.mode === 'preview'
            break
          default:
            tool.disabled = false
            break
        }
        return tool
      });
});

onMounted(()=>{
  document.addEventListener('click', (e) => {
      if (e.target.className === 'lf-mini-map-close') {
        currentTool.value = ''
      }
    }, true)
});

watch(
  () => props.lf,
  () => {
    props.lf && props.lf.on('selection:selected', () => {
      currentTool.value = ''
      props.lf.extension.selectionSelect.closeSelectionSelect()
    })
  },
  {
    immediate: true,
  }
);

watch(
  () => props.graph,
  () => {
    props.graph && props.graph.context.eventCenter.on(EDITOR_EVENT.LOGIC_HISTORY_CHANGE, ({canUndo, canRedo}) => {
        undoAble.value = canUndo
        redoAble.value = canRedo
    })
  },
  {
    immediate: true,
  }
);

function handleClick (item, e) {
    currentTool.value = ''
    if (item.disabled) return
    const type = item.name
    switch (type) {
      case 'beautify':
        props.lf.extension.dagre && props.lf.extension.dagre.layout({
          nodesep: 2,
          ranksep: 20,
          begin: [100, 100],
        });
        break;
      case 'redo':
        props.graph &&props.graph.redo()
        break;
      case 'undo':
        props.graph &&props.graph.undo()
        break;
      case 'zoomIn':
        props.lf.zoom(true);
        break;
      case 'zoomOut':
        props.lf.zoom(false);
        break;
      case 'fitView':
        props.lf.fitView(100, 100);
        break; 
      case 'selection':
        currentTool.value = type
        props.lf.extension.selectionSelect.openSelectionSelect()
        break
      case 'navigation':
        currentTool.value = type
        const position = props.lf.getPointByClient(e.x, e.y)
        props.lf.extension.miniMap.show(position.domOverlayPosition.x - 120, 40)
        break;   
    }
  }
</script>

<style lang="less" scoped>
 .toolbar-wrap {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 36px;
    background: #FFFFFF;
    border-bottom: 1px solid #e4e7ed;
    font-family: PingFangSC-Medium;
    font-size: 12px;
    color: #C1C3C8;
    font-weight: 500;
  }
  .text {
    display: inline-flex;
    height: 100%;
    align-items: center;
    margin-left: 16px;
    max-width: 450px;
    text-overflow: ellipsis;
    overflow: hidden;
    position: absolute;
    left: 0;
  }
  .toolbar {
    height: 100%;
    display: flex;
    align-items: center;
    position: absolute;
    right: 0;
    background: #fff;
  }
  .tool-item {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    width: 24px;
    height: 24px;
    margin-right: 16px;
    border-radius: 4px;
    overflow: hidden;
    cursor: pointer;
    user-select: none;
    img {
      width: 20px;
      height: 20px;
      filter: drop-shadow(#303A51 100px 0);
      transform: translateX(-100px);
    }
    &:hover, &.selected {
      background: #2961ef;
      img {
        filter: drop-shadow(#fff 100px 0);
        transform: translateX(-100px);
      }
    }
    &.disabled {
      background: #fff;
      img {
        filter: drop-shadow(#C1C3C8 100px 0);
        transform: translateX(-100px);
      }
    }
  }
</style>